package com.shop.advertisement.service.impl;

import com.shop.advertisement.service.SearchSlideAdService;
import com.shop.dao.LdbzSearchSlideAdMapper;
import com.shop.pojo.LdbzSearchSlideAd;
import com.shop.utils.FastJsonConvert;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

public class SearchSlideAdServiceImpl implements SearchSlideAdService {
    private static final Logger logger = LoggerFactory.getLogger(SearchSlideAdServiceImpl.class);
    @Autowired
    private RedisTemplate redisTemplate;
    @Value("${redisKey.searchLeft.info.key}")
    private String SEARCH_LEFT_INFO_KEY;

    @Value("${redisKey.expire_time}")
    private Integer REDIS_EXPIRE_TIME;
    @Autowired
    LdbzSearchSlideAdMapper mapper;

    /**
     * 获取搜索页轮播广告页面
     *
     * @param entity
     * @param start
     * @param limit
     * @return
     */
    @Override
    public List<LdbzSearchSlideAd> getSearchSlideAdPage(LdbzSearchSlideAd entity, int start, int limit) {
        return mapper.getSearchSlideAdPage(entity, start, limit);
    }

    /**
     * 获取搜索页轮播广告数量
     *
     * @param entity
     * @return
     */
    @Override
    public long countByEntity(LdbzSearchSlideAd entity) {
        return mapper.countByEntity(entity);
    }

    /**
     * 根据id获取搜索页轮播广告
     *
     * @param id
     * @return
     */
    @Override
    public LdbzSearchSlideAd selectByKey(Long id) {
        return mapper.selectByKey(id);
    }

    /**
     * 删除搜索页轮播广告
     *
     * @param id
     * @return
     */
    @Override
    public int deleteByKey(Long id) {
        return mapper.deleteByKey(id);
    }

    /**
     * 批量删除搜索页轮播广告
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteByKeys(List<Long> ids) {
        return mapper.deleteByKeys(ids);
    }

    /**
     * 添加搜索页轮播广告
     *
     * @param entity
     * @return
     */
    @Override
    public int insertByEntity(LdbzSearchSlideAd entity) {
        return mapper.insertByEntity(entity);
    }

    /**
     * 更新搜索页轮播广告
     *
     * @param entity
     * @return
     */
    @Override
    public int updateByKey(LdbzSearchSlideAd entity) {
        return mapper.updateByKey(entity);
    }

    /**
     * 获取搜索页轮播广告
     *
     * @return
     */
    @Override
    public List<LdbzSearchSlideAd> getSearchSlideAdByRedis() {
        logger.debug("getSearchSlideAdByRedis");
        try {
            String json = String.valueOf(redisTemplate.opsForValue().get(SEARCH_LEFT_INFO_KEY));
            if (StringUtils.isNotBlank(json)) {
                logger.info("Redis 获取检索页广告");
                return FastJsonConvert.convertJSONToArray(json, LdbzSearchSlideAd.class);
            } else {
                logger.error("Redis 查询不到检索页广告");
            }
        } catch (Exception e) {
            logger.error("检索也广告缓存报错", e);
        }
        List<LdbzSearchSlideAd> ret = mapper.selectByEntity(new LdbzSearchSlideAd(), 0, Integer.MAX_VALUE);
        try {
            logger.info("Redis 更新检索页广告信息");
            redisTemplate.opsForValue().set(SEARCH_LEFT_INFO_KEY, FastJsonConvert.convertObjectToJSON(ret));
            redisTemplate.getExpire(SEARCH_LEFT_INFO_KEY);
        } catch (Exception e) {
            logger.error("Redis 更新检索页广告缓存信息报错", e);
        }
        return ret;
    }
}
